﻿#include "battarywidget.h"

BattaryWidget::BattaryWidget(QWidget* parent) : QWidget(parent), value_(0), edge_(10), diameter_(20), pen_width_(10) {
  //设置电池大小
  // setFixedSize(26, 16);

  //充电动画
  charge_timer_ = new QTimer();
  connect(charge_timer_, &QTimer::timeout, this, [this]() {
    static int cnt;
    this->setValue(cnt++);
    if (cnt == 100) {
      cnt = 0;
    }
  });
  setStyleSheet("border: 3px solid rgb(0, 198, 198);");
}

/**
 * @brief BattaryWidget::setValue 刷新电量值
 * @param value
 */
void BattaryWidget::setValue(int value) {
  value_ = value;
  update();
}

void BattaryWidget::setPercentValue(int percent) {
  percent_value = percent;
  //   update();
}

/**
 * @brief BattaryWidget::showChargeAnimate 设置是否显示充电动画
 * @param isShow
 */
void BattaryWidget::showChargeAnimate(bool isShow) { isShow ? charge_timer_->start(100) : charge_timer_->stop(); }

void BattaryWidget::paintEvent(QPaintEvent* event) {
  Q_UNUSED(event)
  //使用QPainterPath画圆角矩形.注意,如要修改请认真阅读arcTo()函数
  QPainterPath round_rect_path;
  round_rect_path.moveTo(this->width() - 2 * edge_, edge_ + diameter_);
  round_rect_path.arcTo(this->width() - 2 * edge_ - diameter_, edge_, diameter_, diameter_, 0.0, 90.0);
  round_rect_path.lineTo(edge_ + diameter_, edge_);
  round_rect_path.arcTo(edge_, edge_, diameter_, diameter_, 90.0, 90.0);
  round_rect_path.lineTo(edge_, this->height() - edge_ - diameter_);
  round_rect_path.arcTo(edge_, this->height() - edge_ - diameter_, diameter_, diameter_, 180.0, 90.0);
  round_rect_path.lineTo(this->width() - 2 * edge_ - diameter_, this->height() - edge_);
  round_rect_path.arcTo(this->width() - 2 * edge_ - diameter_, this->height() - edge_ - diameter_, diameter_, diameter_, 270.0, 90.0);
  round_rect_path.closeSubpath();

  //画外层边框
  QPainter painter(this);
  QColor border_color = QColor(50, 50, 50);
  painter.setPen(QPen(border_color, pen_width_));
  painter.drawPath(round_rect_path);

  //画电池头
  painter.setBrush(border_color);
  painter.drawRoundedRect(this->width() - 1.5 * edge_, this->height() / 2 - (this->height() - 2 * (edge_ + diameter_)) / 2 / 2, edge_ / 2,
                          (this->height() - 2 * (edge_ + diameter_)) / 2, 2, 2);

  //画电池百分比.充电时显示绿色,多于50显示绿色,25-50显示棕色,少于25显示红色
  QPen pen(Qt::SolidLine);
  QLinearGradient gradient(0, 0, this->width(), 0);
  if (charge_timer_->isActive()) {
    pen.setColor(QColor(0, 255, 0));
    gradient.setColorAt(1.0, QColor(0, 180, 0));
    gradient.setColorAt(0.0, QColor(0, 220, 0));
  } else if (value_ > 50) {
    pen.setColor(QColor(0, 255, 0));
    gradient.setColorAt(1.0, QColor(0, 180, 0));
    gradient.setColorAt(0.0, QColor(0, 220, 0));
  } else if (value_ >= 25) {
    pen.setColor(QColor(206, 92, 0));
    gradient.setColorAt(1.0, QColor(206, 92, 0));
    gradient.setColorAt(0.0, QColor(255, 110, 0));
  } else if (value_ > 0) {
    pen.setColor(QColor(240, 0, 0));
    gradient.setColorAt(1.0, QColor(200, 0, 0));
    gradient.setColorAt(0.0, QColor(240, 0, 0));
  } else {
    pen.setColor(QColor(255, 255, 255));
    gradient.setColorAt(1.0, QColor(255, 255, 255));
    gradient.setColorAt(0.0, QColor(255, 255, 255));
  }
  painter.setPen(pen);
  painter.setBrush(gradient);
//  painter.drawRoundedRect(edge_ + diameter_ / 2 + pen_width_ / 2, edge_ + diameter_ / 2 + pen_width_ / 2,
//                          (this->width() - 2 * (edge_ + diameter_ / 2 + pen_width_ / 2)) * value_ / 100,
//                          this->height() - 2 * (edge_ + diameter_ / 2 + pen_width_ / 2), 0, 0);
  painter.drawRoundedRect(edge_ + diameter_ / 4 + pen_width_ / 4, edge_ + diameter_ / 4 + pen_width_ / 4,
                          (this->width() - 2 * (edge_ + diameter_ / 4 + pen_width_ / 4)) * value_ / 100,
                          this->height() - 2 * (edge_ + diameter_ / 4 + pen_width_ / 4), 0, 0);

  //画电量显示文本
  pen.setColor(QColor("black"));
  painter.setPen(pen);
  QFont font = painter.font();
  font.setPixelSize(38);
  painter.setFont(font);
  painter.drawText(0, 0, this->width(), this->height(), Qt::AlignCenter, QString("%1%").arg(percent_value));
}

int BattaryWidget::value() const { return value_; }
